#ifndef _SLAB_ALLOCATOR_H_
#define _SLAB_ALLOCATOR_H_

/*
 * A fast and simple slab allocator designed to work on top of malloc/free --
 * handy if your code uses many quick allocations and deallocations of
 * statically-sized structures and the underlying memory management facilities
 * are not fast enough to meet your needs.
 *
 * This slab allocator will be able to run on top of any generic malloc/free
 * implementation and makes absolutely no assumptions about the underlying
 * memory management algorithms, layouts, or implementations. It is portable
 * to many systems and architectures.
 *
 * The slab allocator was tuned for outright speed and lowest possible memory
 * overhead (both code and data) per object and overall. The design will
 * naturally resist fragmentation over time and attempt to coalesce allocations
 * into the smallest number of slabs possible.
 *
 * The code was written for and tested on both 32-bit and 64-bit systems,
 * although it was heavily optimized for the 32-bit case to the point where
 * memory usage per object is pessimized in the 64-bit case.
 */

#include <pthread.h>

struct linked_list {
	struct linked_list * next;
	struct linked_list * prev;
};

struct slab_allocator {
	pthread_mutex_t global_lock;
	struct linked_list slabs_list;
	unsigned int object_count;
	unsigned int object_size;
	int canary_check;
};

void slab_create(struct slab_allocator * allocator,
		 unsigned int objcount,
		 unsigned int objsize,
		 int canary);
void * slab_alloc(struct slab_allocator * allocator);
void slab_free(struct slab_allocator * allocator,
	       void * object);

#endif
